﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;
namespace StudentWeb.Api.Controllers
{
    public class UserController : ApiController
    {
        UserBO _userBO = new UserBO();
        #region 用户信息 登录
        [HttpPost]
        public IHttpActionResult Login([FromBody]JObject obj)
        {
            dynamic model = obj;
            string username = model.username ?? "";
            string password = model.password ?? "";
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return Json(new Result(ResultCode.Fail, "用户名或密码不能为空"));
            }
            try
            {
                var result = _userBO.Login(username, password);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 用户信息 保存
        [HttpPost]
        public IHttpActionResult SaveUserInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            string UserName = model.UserName ?? "";
            string PassWord = model.PassWord ?? "";
            if (string.IsNullOrEmpty(UserName))
            {
                return Json(new Result(ResultCode.Fail, "用户名不能为空"));
            }
            if (string.IsNullOrEmpty(PassWord))
            {
                return Json(new Result(ResultCode.Fail, "密码不能为空"));
            }
            try
            {
                var result = _userBO.SvaeUserInfo(model);
                return result ? Json(new Result(ResultCode.Success, "保存成功")) :
                Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 用户信息 全部记录
        [HttpPost]
        public IHttpActionResult GetUserAllList()
        {
            try
            {
                var result = _userBO.GetUserAllList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 用户信息 添加
        [HttpPost]
        public IHttpActionResult InsertUserInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            string UserName = model.UserName ?? "";
            string PassWord = model.PassWord ?? "";
            if (string.IsNullOrEmpty(UserName))
            {
                return Json(new Result(ResultCode.Fail, "用户名不能为空"));
            }
            if (string.IsNullOrEmpty(PassWord))
            {
                return Json(new Result(ResultCode.Fail, "密码不能为空"));
            }
            try
            {
                var result = _userBO.InsertUserInfo(model);
                return result ? Json(new Result(ResultCode.Success, "添加成功")) :
                Json(new Result(ResultCode.Fail, "添加失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 用户信息 单条记录
        [HttpPost]
        public IHttpActionResult GetUserInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int UserDataID = model.UserDataID ?? 0;
            if (UserDataID == 0)
            {
                return Json(new Result(ResultCode.Fail, "ID 不能为空"));
            }
            try
            {
                var result = _userBO.GetUserInfo(UserDataID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 用户信息 删除
        [HttpPost]
        public IHttpActionResult DeleteUserInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int UserDataID = model.UserDataID ?? 0;
            if (UserDataID == 0)
            {
                return Json(new Result(ResultCode.Fail, "ID 不能为空"));
            }
            try
            {
                var result = _userBO.DeleteUserInfo(UserDataID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 用户信息 分页列表
        [HttpPost]
        public IHttpActionResult GetUserList([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail,
                "参数错误"));
                var wc = _GetUserListWhereC(model.condidate);
                var result = _userBO.GetUserList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _GetUserListWhereC(dynamic qc)
        {
            var wc = string.Empty;
            if (qc == null) return wc;
            string tmp = "";
            tmp = ConvertHelper.DynamicToString(qc.UserName);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" UserName like '%{tmp}%'"; }
            return wc;
        }
        #endregion
    }
}